<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          v-model="categoryStore.c1Id"
          @change="handlerC2"
          :disabled="scene == 0 ? false : true"
        >
          <el-option
            v-for="c1 in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c2Id"
          @change="handlerC3"
        >
          <el-option
            v-for="c2 in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="c3 in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
// 引入生命周期函数
import { onMounted } from 'vue'
// 引入分类仓库
import useCategoryStore from '@/store/modules/category'

// 获取分类仓库
const categoryStore = useCategoryStore()

// 组件挂载完毕
onMounted(() => {
  categoryStore.c1Id = ''
  categoryStore.c2Id = ''
  categoryStore.c3Id = ''
  getC1()
})

// 通知仓库发送请求获取一级分类的数据
const getC1 = () => {
  categoryStore.getC1()
}
// 一级分类下拉菜单的change事件->产生一级分类id
const handlerC2 = () => {
  // 每次修改时，需要将二级、三级分类数据清空
  categoryStore.c2Id = ''
  categoryStore.c3Id = ''
  categoryStore.c3Arr = []
  // 通知仓库获取一级分类id
  categoryStore.getC2()
}
// 二级分类下拉菜单的change事件->产生二级分类id
const handlerC3 = () => {
  // 每次修改时，需要将三级分类的id
  categoryStore.c3Id = ''
  // 通知仓库获取二级分类id
  categoryStore.getC3()
}
// 接收父组件传递来的scene
defineProps(['scene'])
</script>

<style scoped>
.el-select {
  --el-select-width: 220px;
}
</style>